<?php
namespace app\home\logic;

use app\common\logic\Base;
use think\facade\App;
use think\facade\Hook;
use think\facade\Request;

class Domain extends  Base {
    
    /**
     * @desc 我的订单
     * @param $params
     * @return array
     * @throws \Exception
     */
    public function index($params) {
        $where = [
            ['domain_user_id', '=', $params['userId']],
        ];
        $list = App::model('Domain')
            ->where($where)
            ->order('domain_id', 'desc')
            ->paginate($params['pageSize']);
        $pagination = $list->render();
        
        if($list) {
            $list = $list->toArray();
            $list = output_format($list['data'], 'domain');
        }

        return [
            'list'               => $list,
            'pagination'         => $pagination
        ];
    }
    
    /**
     * @desc 添加域名
     * @param array $params
     * @param int $userId
     * @return bool
     * @throws \Exception
     */
    public function operateAjax($params, $userId) {
        if($params['httpProtocol'] == 'https') {
            if($params['httpsPem'] == '' || $params['httpsKey'] == '') {
                $this->errorCode = EC_HOME_PLEASE_INPUT_HTTPS_CERTIFICATE;
                return false;
            }
        }
    
        $domainInfo = model('Domain')->where('domain_name', '=', $params['domainName'])->find();
        if($domainInfo) {
            $this->errorCode = EC_HOME_DOMAIN_INFO_ALREADY_EXIST;
            return false;
        }
        
        $insertData = [
            'domain_user_id'       => $userId,
            'domain_name'          => $params['domainName'],
            'domain_http_protocol' => $params['httpProtocol'],
            'domain_https_pem'     => $params['httpsPem'],
            'domain_https_key'     => $params['httpsKey'],
            'domain_status'        => DOMAIN_STATUS_NO,
            'domain_modify_time'   => date('Y-m-d H:i:s'),
            'domain_create_time'   => date('Y-m-d H:i:s'),
        ];
        $result = model('Domain')->insert($insertData);
        if($result === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
        //发送站内信
        model('Message')->insert([
            'msg_user_id'  => $userId,
            'msg_contents' => '域名“'. $params['domainName'] . '”添加成功！',
            'msg_type'     => MESSAGE_TYPE_USER,
            'msg_status'   => MESSAGE_STATUS_UNREAD
        ]);
        
        return true;
    }
    
    /**
     * @desc 删除域名
     * @param array $params
     * @param int $userId
     * @return array|bool
     * @throws \Exception
     */
    public function del($params, $userId) {
        $where = [
            ['domain_id', '=', $params['id']],
            ['domain_user_id', '=', $userId],
        ];
        $domainInfo = App::model('Domain')->where($where)->find();
        if(!$domainInfo) {
            $this->errorCode = EC_HOME_DOMAIN_INFO_NOT_EXIST;
            return false;
        }
        
        $result = App::model('Domain')->where('domain_id', '=', $domainInfo['domain_id'])->delete();
        if($result === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
    
        //发送站内信
        model('Message')->insert([
            'msg_user_id'  => $userId,
            'msg_contents' => '域名“'. $domainInfo['domain_name'] . '”删除成功！',
            'msg_type'     => MESSAGE_TYPE_USER,
        ]);
        
        return true;
    }
}
